package com.qs.eggs.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.adapter.Address1Adapter;
import com.qs.eggs.adapter.FooAdapter;
import com.qs.eggs.adapter.FootAdapter;
import com.qs.eggs.base.activity.BaseActivity;
import com.qs.eggs.bean.CollectBean;
import com.qs.eggs.refresh.widget.RecyclerRefreshLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class FootPrint1Activity extends Activity implements View.OnClickListener ,Runnable{
    public static final int ACTION_EDIT = 1;
    public static final int ACTION_CAMPLATE = 2;
    private RecyclerView recyclerView;
    private FootAdapter footAdapter;
    private TextView mEdit;
    protected RecyclerRefreshLayout mRefreshLayout;
    protected boolean isRefreshing;
    private ImageView fanhui;
    public static Activity instance;
    List<Map<String,Object>> footlist=null;
    private FooAdapter fooAdapter;
    private Handler mHandler;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance=this;
        Thread thread = new Thread(this);
        thread.start();
        setContentView(R.layout.activity_foot_print1);
        initView();
        getData();
    }

    private void initView(){
    recyclerView = (RecyclerView) findViewById(R.id.collect_recyclerview);
    mEdit = (TextView) findViewById(R.id.tv_edit);
    fanhui = (ImageView) findViewById(R.id.fanhui);
    mEdit.setOnClickListener(this);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    fanhui.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FootPrint1Activity.this.finish();
        }
    });
}

    //获得我的足迹列表
    public void getData(){
        OkGo.post(Constants.URL_BASE + "lookhistory?action=searchLookHistory")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("我的足迹==" + s);
                        try {
                            footlist=new ArrayList<>();
                            JSONArray jsonArray = new JSONArray(s);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                Map<String, Object> map = new HashMap<>();
                                map.put("collectid", jsonObject.getString("collectid"));
                                map.put("type", jsonObject.getString("type"));
                                map.put("proid", jsonObject.getString("proid"));
                                map.put("proname", jsonObject.getString("proname"));
                                map.put("saleprice", jsonObject.getString("saleprice"));
                                map.put("autoname", jsonObject.getString("autoname"));
                                map.put("count", jsonObject.getString("count"));
                                footlist.add(map);

                            }

                            fooAdapter = new FooAdapter(FootPrint1Activity.this, footlist,mHandler);
                            recyclerView.setAdapter(fooAdapter);


                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                });
}
    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.tv_edit:
               FootPrint1Activity.this.finish();
               break;


       }

    }
    @Override
    public void run() {
        Looper.prepare();
        mHandler = new Handler(){
            public void handleMessage(android.os.Message msg) {
                switch (msg.what) {
                    case 5:
                        getData();
                        break;
                    default:
                        break;
                }
            }
        };
        Looper.loop();
    }
}
